package com.mac.bio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

/**
 * @Description
 * @auther machuan
 * @create 2020-04-17 15:07
 */
public class ExecSocket implements Runnable {

    private Socket socket;

    ExecSocket(Socket socket) {

        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            //接收到请求后使用socket通信
            BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            System.out.println("waiting read........");
            String line = reader.readLine();
            System.out.println("收到客户端信息：" + line);
            //向客户端发送信息
            PrintWriter pw = new PrintWriter(socket.getOutputStream());
            pw.println("我收到了信息：" + line);
            pw.flush();
            pw.close();
            reader.close();

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
